package com.igs.parse.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.igs.parse.query.QueryCountUtil;
import com.loyin.model.DownloadUrl;
import com.loyin.model.WebSite;
import com.loyin.tool.AnalysisUrl;

public class PraseWebsite
{
	private static Logger log = Logger.getLogger(downLoadWebsite.class);

	public static void main(String[] args) 
	{
		try
		{
			log.info("分析网站定时调度开始。。。。。。。。。。。");
		int i=0;
		int pageCount=10;
		while(true)
		{
			String pageSql=" 1=1 ORDER BY id asc  LIMIT "+i+", "+pageCount+" ";
			List<WebSite> websiteList = (List<WebSite>) new WebSite().findAll(WebSite.class, pageSql);
			if(websiteList==null || websiteList.size()==0)
				break;
			//下载网页
			for(WebSite website :websiteList)
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String recordtime = sdf.format(new Date());
				long userid = website.userid;
				AnalysisUrl aurl = new AnalysisUrl();
				List<Map<String, Object>> list = aurl.analysis(website.url, website.keyword);
				int count = 0;
				if (list != null && list.isEmpty() == false)
				{
					for (Map<String, Object> mp : list)
					{
						DownloadUrl durl = new DownloadUrl(userid, website.id, mp.get("title").toString(), mp.get("href").toString(), Integer.parseInt(mp.get("count").toString()), recordtime);
						try
						{
							//先判断是否已经有了
							List<DownloadUrl> dlist = (List<DownloadUrl>) durl.findBySql(durl.getClass(), 
									"select * from igs_downloadurl where userid=? and websiteid =? and title=? ", 
									new Object[]{ durl.getUserid(), durl.getWebsiteid(), durl.getTitle() });
							//没有则增加
							if(dlist==null || dlist.size()==0)
							{
								durl.save();
								count++;
							}
						} catch (Exception e)
						{
							log.error("分析一个网站异常", e);
						}
					}
				}
			}
			i=i+pageCount;
		}
		log.info("分析网站定时调度完成。。。。。。。。。。。");
		}catch(Exception ex)
		{
			log.error("分析网站异常", ex);
		}
		QueryCountUtil.isTodayDownLoad=true;
		try
		{
			downLoadWebsite.main(null);
		}
		catch(Exception ex)
		{
			log.error("下载网站异常", ex);
		}
	}
}
